﻿Public Class oPromotionItemPoint
    Public Function UpdateJsonPromotionItemPoint(ByVal id As String, ByVal promotionID As String, _
                                   ByVal sourceType As String, ByVal sourceValue As String, _
                                   ByVal getPoint As Double, ByVal basedValue As Double, ByVal basedType As String, _
                                   ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {promotionID, sourceType, sourceValue, getPoint, basedValue, basedType}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oPromotionItemPoint]")
                    query.Append("  ([PromotionID]")
                    query.Append(" ,[SourceType]")
                    query.Append(" ,[SourceValue]")
                    query.Append(" ,[GetPoint]")
                    query.Append(" ,[BasedValue]")
                    query.Append(" ,[BasedType])")
                    query.Append(" VALUES (@Param1,@Param2,@Param3,@Param4,@Param5,@Param6)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oPromotionItemPoint] SET ")
                    query.Append("  [SourceType] = @Param1")
                    query.Append("  ,[SourceValue] = @Param2")
                    query.Append("  ,[GetPoint] = @Param3")
                    query.Append("  ,[BasedValue] = @Param4")
                    query.Append("  ,[BasedType] = @Param5")
                    query.Append(" WHERE [ID] = @Param6")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {sourceType, sourceValue, getPoint, basedValue, basedType, id})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonPromotionItemPoint(ByVal dataBaseName As String, ByVal id As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {id}
                Dim query As New StringBuilder
                query.Append(" DELETE FROM [oPromotionItemPoint]")
                query.Append("  WHERE ID =  @Param1")
                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllPromotionItemPointByPromotionID(ByVal dataBaseName As String, ByVal promotionID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oPromotionItemPoint")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            Dim query As New StringBuilder
            If errMsg.Length = 0 Then
                query.Append("  SELECT * FROM (")
                query.Append(" (SELECT T0.*")
                query.Append(" ,T1.ReferenceName AS ItemSourceTypeName")
                query.Append(" ,T2.ReferenceName AS BasedTypeName")
                query.Append(" ,T4.ItemName AS SourceValueName")
                query.Append(" FROM oPromotionItemPoint T0 LEFT JOIN oReferenceValue T1 ON (T0.SourceType = T1.ReferenceValue AND T1.ReferenceCode  = 'ItemSourceType' )")
                query.Append(" LEFT JOIN oReferenceValue T2 ON (T0.BasedType = T2.ReferenceValue AND T2.ReferenceCode  =  'BasedType')")
                query.Append(" JOIN oItem T4 ON T4.ItemID = T0.SourceValue")
                query.Append(" WHERE T1.ReferenceCode = 'ItemSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Item Code')")
                query.Append(" UNION")
                query.Append(" (SELECT T0.*")
                query.Append(" ,T1.ReferenceName AS ItemSourceTypeName")
                query.Append(" ,T2.ReferenceName AS BasedTypeName")
                query.Append(" ,T4.PropertyDetailName AS SourceValueName")
                query.Append(" FROM oPromotionItemPoint T0 LEFT JOIN oReferenceValue T1 ON (T0.SourceType = T1.ReferenceValue AND T1.ReferenceCode  = 'ItemSourceType' )")
                query.Append(" LEFT JOIN oReferenceValue T2 ON (T0.BasedType = T2.ReferenceValue AND T2.ReferenceCode  =  'BasedType')")
                query.Append(" JOIN oItemPropertyDetail T4 ON T4.PropertyDetailID = T0.SourceValue")
                query.Append(" WHERE T1.ReferenceCode = 'ItemSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Item Property' )")
                query.Append(" UNION")
                query.Append(" (SELECT T0.*")
                query.Append(" ,T1.ReferenceName AS ItemSourceTypeName")
                query.Append(" ,T2.ReferenceName AS BasedTypeName")
                query.Append(" ,SourceValue AS SourceValueName")
                query.Append(" FROM oPromotionItemPoint T0 LEFT JOIN oReferenceValue T1 ON (T0.SourceType = T1.ReferenceValue AND T1.ReferenceCode  = 'ItemSourceType' )")
                query.Append(" LEFT JOIN oReferenceValue T2 ON (T0.BasedType = T2.ReferenceValue AND T2.ReferenceCode  =  'BasedType')")
                query.Append(" WHERE T1.ReferenceCode = 'ItemSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Query String')")
                query.Append(" ) AS T0")
                query.Append(" ORDER BY ID DESC")

                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {promotionID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
